var stickyAdObject = function(config){
	var _root = this,
		settings = {
			creative: false,
			height: 48,
			width: $(document).width(),
			position : "bottom",
			maxSafeWidth : 980
		},
		errorLevel = new Array(
			false,
			"ERROR: No creative set",
			"WARNING: Width exceeds max safe width."
		),
		timestamp,
		adContainerId,
		adId,
		$stickyAd,
		$stickyAdContainer,
		$dummyBlock;
	var isUrl = function(subject) {
		log(subject);
        if (typeof(subject) == "string") {
            var url_pattern = new RegExp("((http|https)(:\/\/))?([a-zA-Z0-9-_]+[.]{1}){2}[a-zA-z0-9-_]+(\/{1}[a-zA-Z0-9-_]+)*\/?", "i");
            return subject.match(url_pattern);
        }
        else {
            return false;
        }
    },
	createAndApendAd = function(){
		var adContent = settings.creative;
		$stickyAd = $("<div/>");
		$stickyAd.attr("id", adId);
		if(isUrl(adContent)){
			adContent = returnIframeFromDataset(settings);
		}
		$stickyAd.addClass("sticky-ad");
		$stickyAd.css({
			height : settings.height,
			width : settings.width,
			zIndex: 12000,
			position: "relative"
		});
		$stickyAd.html(adContent);
		$stickyAdContainer.append($stickyAd);
	},
	returnIframeFromDataset = function(creativeDataset) {
        var $iframe = $("<iframe/>");
        $iframe.attr({
            src: creativeDataset.creative,
            width: "100%",
            height: creativeDataset.height,
            frameborder: 0,
            allowtransparency: "true",
            scrolling: "no",
			marginwidth: 0,
			marginheight: 0
        });
        $iframe.css({
            "background": "transparent"
        });
        return $iframe;
    },
	createAndApendContainer = function(){
		$stickyAdContainer = $("<div>");
		$stickyAdContainer.attr("id", adContainerId)
		$stickyAdContainer.addClass("sticky-ad-container");
		$stickyAdContainer.css({
			position: "absolute",
			width: "100%"
		});
		$("body").prepend($stickyAdContainer);
	}
	prepearConfig = function() {
        $.extend(true, settings, config);
        if (settings.maxSafeWidth) {
            if (settings.width > settings.maxSafeWidth) {
                settings.width = settings.maxSafeWidth;
            }
        }
    },
    positionAd = function(){
    	positionVertical();
    },
    positionVertical = function(){
    	var position = settings.position;
    	if(position == "top"){
    		var offsetFromTop = $(window).scrollTop();
    	}
    	else {
    		var offsetFromTop = $(window).scrollTop() + $(window).height() - $stickyAd.height();
    	}
    	$stickyAdContainer.css("top", offsetFromTop );
    },
    log = function(data){
    	if(console.log){
    		console.log(data);
    	}
    },
    createAndAppendAdSpacer = function(){
    	$dummyBlock = $("<div/>");
    	$dummyBlock.height($stickyAd.height());
    	$("body").append($dummyBlock);
    },
    __construct = function(){
		timestamp = new Date().getTime();
		adContainerId = "sticky-ad-container-" + timestamp;
		adId = "sticky-ad-" + timestamp;
		prepearConfig();
    	if(settings.creative){
	    	createAndApendContainer();
	    	createAndApendAd();
	    	createAndAppendAdSpacer();
	    	positionAd();
	    	$(window).scroll(function(){ positionAd() });
	    	$(window).resize(function(){ positionAd() });
    	}
    	else {
    		log(errorLevel[1]);
    		return false;
    	}
    };
    this.ad = function(){
    	return $("#" + adId);
    }
    this.container = function(){
    	return $("#" + adContainerId);
    }
    __construct();
}